import { _decorator, AssetManager, Component, director, DynamicAtlasManager, Game, game, macro, Node } from 'cc';
import { LogUtil } from '../Util/LogUtil';
import { MultiPlatform, Platform } from '../Basic/Model/Platform/MultiPlatform';
import { PlayerParams } from '../Basic/Config/DataConfig';
import { WebRequestHttp, RequestUrl } from '../Basic/Handle/WebRequest';
import { PlayerData, DataMgr } from '../Basic/Manager/DataManager';
import { GravityModel, GEEvent } from '../Basic/Model/SDK/GravityModel';
import { EventManager } from '../Basic/Manager/EventManager';
import { EventType } from '../Game/Config/GameEnum';
import { LevelModel } from '../Game/Level/LevelModel';
const { ccclass } = _decorator;
/* eslint-disable */
export var GameControler: Node = null!;

@ccclass('MainScene')
export class MainScene extends Component {
  protected onEnable(): void {
    // EventManager.Ins().register(EventType.LOGIN, this.requestServer, this);
  }

  start(): void {
    EventManager.Ins().register(EventType.LOGIN, this.onClickStartGame, this);
    DynamicAtlasManager.instance.maxFrameSize = 512;
    macro.CLEANUP_IMAGE_CACHE = false;
    // DynamicAtlasManager.instance.enabled = true;
    //多点触碰
    macro.ENABLE_MULTI_TOUCH = false;
    DataMgr.init();
    MultiPlatform.Ins().platformSelect();
    AssetManager.Downloader.instance.maxConcurrency = 25;
    AssetManager.Downloader.instance.maxRequestsPerFrame = 5;
    director.addPersistRootNode(this.node);
    GameControler = this.node;
    game.on(Game.EVENT_SHOW, this.gameShow, this);
    game.on(Game.EVENT_HIDE, this.gameHide, this);
    LogUtil.i('MainScene', 'main set finish');
    // this.requestServer();
  }

  onClickStartGame() {
    LogUtil.i('开始进入游戏');
    // GameControl.Ins().readyToLoading();
    LevelModel.Ins().loadTime = Date.now();
    // GravityModel.I.setupAndStart();
    // GravityModel.I.track(GEEvent.GAME_START, { isFirst: PlayerData.get(PlayerParams.isFirst) });
    if (PlayerData.get(PlayerParams.isFirst)) {
      const date = new Date();
      const dateTime = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
      // GravityModel.I.userSetOnce({ firstOpenDate: dateTime });
      DataMgr.changePlayerAttr(PlayerParams.isFirst, false, true);
    }
    // let time = 0;
    // setInterval(() => {
    //   time++;
    //   GravityModel.I.track(GEEvent.ONLINE_TIME, { PlayTimes: time * 60 });
    // }, 60 * 1000);

    director.loadScene('LoadScene');
  }

  /** 游戏切回前台 */
  gameShow() {
    console.log('游戏进入前台');
    // this.playMusic(SoundCode.BGM);
  }

  /** 游戏进入后台 */
  gameHide() {}

  requestServer(data: any = null) {
    WebRequestHttp.requestGet(RequestUrl.api_url + RequestUrl.LOGIN, this.reqLogin(data), this.respLogin, this);
  }

  //['code', 'platform'];
  private reqLogin(data?: any) {
    let obj = null;
    switch (MultiPlatform.Ins().platform) {
      case Platform.wx:
      case Platform.tt:
        obj = { code: data.code, platform: MultiPlatform.Ins().platform };
        break;
      // case Platform.TTKey:
      case Platform.h5:
        obj = { code: PlayerData.get(PlayerParams.open_id), platform: MultiPlatform.Ins().platform };
        break;
    }
    console.log('登录请求数据：', obj);
    return obj;
  }

  private respLogin(data?: any) {
    LogUtil.i('登录返回数据：', data);
    switch (MultiPlatform.Ins().platform) {
      case Platform.tt:
      case Platform.wx:
        if (!data || !data.data || !data.data.sessionKey) break;
        DataMgr.changePlayerAttr(PlayerParams.session_key, data.data.sessionKey, true);
        DataMgr.changePlayerAttr(PlayerParams.open_id, data.data.openid, true);
        break;
      default:
        if (!data || !data.sessionKey) break;
        DataMgr.changePlayerAttr(PlayerParams.session_key, data.sessionKey, true);
        DataMgr.changePlayerAttr(PlayerParams.open_id, data.openid, true);
        break;
    }

    // director.loadScene('LoadScene');
    // getOpemId
    // const url = 'https://dyplatform.cygame666.cn';
    // switch (MultiPlatform.Ins().platform) {
    //   case Platform.h5:
    this.onClickStartGame();
    // break;
    //   case Platform.tt:
    //     WebRequestHttp.requestPost(
    //       url + '/platform_go_bytedance/get/openId',
    //       this.reqOpenId({ appId: 'ttb10ac7b72e3ad75002', code: MultiPlatform.Ins().platformUnit.$appCode }),
    //       this.respOpenId,
    //       this
    //     );
    //     break;
    //   default:
    //     break;
    // }
  }
}
